package com.stars.services.impl.system;/**
 * Created by luodajie on 2019/1/5.
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stars.common.base.BaseConstant;
import com.stars.common.result.VueSelectItem;
import com.stars.common.result.VueTreeItem;
import com.stars.dao.mapper.system.SystemUserRoleMapper;
import com.stars.entity.ListPage;
import com.stars.entity.sys.SystemUserRole;
import com.stars.services.impl.BaseAccessServiceImpl;
import com.stars.services.system.UserRoleAccessService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:39
 * @note
 */
@Service
public class UserRoleAccessServiceImpl extends BaseAccessServiceImpl<SystemUserRoleMapper, SystemUserRole> implements
        UserRoleAccessService {
    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;

    @Override
    public SystemUserRole findByEntityId(String id) throws Exception {
        return systemUserRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SystemUserRole> findByPage(Integer pageNum, Integer pageSize, SystemUserRole command) throws
            Exception {
        if(StringUtils.isBlank(command.getPid()) && StringUtils.isBlank(command.getRoleName())){
            command.setPid(BaseConstant.BASE_ROOT);
        }
        ListPage<SystemUserRole> listPage = new ListPage<>(pageNum, pageSize, command);
        PageHelper.startPage(pageNum, pageSize);
        List<SystemUserRole> systemUserRoleList = systemUserRoleMapper.findByPage(listPage);
        PageInfo<SystemUserRole> pageInfo = new PageInfo<>(systemUserRoleList);
        return pageInfo;
    }

    @Override
    public List<SystemUserRole> findByRoleUserId(String userId) throws Exception {
        return systemUserRoleMapper.findByRoleUserId(userId);
    }

    @Override
    public List<VueSelectItem> findAll() throws Exception {
        List<SystemUserRole> list = systemUserRoleMapper.selectAll();
        List<VueSelectItem> itemList = new ArrayList<>();
        if(null!=list){
            for(SystemUserRole r:list){
                VueSelectItem item = new VueSelectItem();
                if(StringUtils.isNotBlank(r.getId())){
                    item.setValue(r.getId());
                    item.setLabel(r.getRoleName());
                    itemList.add(item);
                }
            }
        }
        return itemList;
    }

    @Override
    public List<VueTreeItem> roleTree() throws Exception {
        List<SystemUserRole> list = systemUserRoleMapper.findByRolePid(BaseConstant.BASE_ROOT);
        List<VueTreeItem> treeList = treeList(list);
        return treeList;
    }

    private List<VueTreeItem> treeList(List<SystemUserRole> list) {
        List<VueTreeItem> vueTreeItemChildList = new ArrayList<>();
        for (SystemUserRole child : list) {
            VueTreeItem vueTreeItemChild = new VueTreeItem();
            vueTreeItemChild.setId(child.getId());
            if ("1".equals(child.getRoleEnabled())) {
                vueTreeItemChild.setDisabled(false);
            }
            vueTreeItemChild.setTitle(child.getRoleName());
            List<SystemUserRole> childList = child.getSystemUserRoles();
            if (null != childList && childList.size() > 0) {
                List<VueTreeItem> vueTreeItemChildList1 = treeList(childList);
                vueTreeItemChild.setChildren(vueTreeItemChildList1);
            }
            vueTreeItemChildList.add(vueTreeItemChild);
        }
        return vueTreeItemChildList;
    }
}
